package hikari.persistence.propertieseditor.editors;

import com.gooddies.texteditors.DefaultFloatTextField;
import com.gooddies.texteditors.validation.AbstractValidator;
import hikari.persistence.file.FileSection;
import java.io.IOException;
import javax.swing.JComponent;

/**
 * @author sad
 */
public class FloatPropertyEditor extends DefaultFloatTextField implements IPropertyEditor {

    @Override
    public Object getEditorValue() {
        return (Float) (float) getValue();
    }

    @Override
    public void setEditorValue(Object value) {
        setValue((float) (Float) value);
    }

    @Override
    public void setMax(Object max) {
        double tMaxValue = Double.parseDouble((String) max);
        setMaxValue(tMaxValue);
    }

    @Override
    public void setMin(Object min) {
        double tMinValue = Double.parseDouble((String) min);
        setMinValue(tMinValue);
    }

    @Override
    public void setValidator(AbstractValidator validator) {
        addValidator(validator);
    }

    @Override
    public JComponent getJComponent() {
        return this;
    }

    @Override
    public void setMaxLength(String length) {
        throw new UnsupportedOperationException("Integer property editor does not support length property");
    }

    @Override
    public void save(String fieldName, FileSection section) throws IOException {
        section.writeFloat(fieldName, (Float) getEditorValue());
    }

    @Override
    public void read(String fieldName, FileSection section) throws IOException {
        Float value=section.readFloat(section.getChunk(fieldName));
        setEditorValue(value);
    }
}
